Add convenience function for querying a child of GtkGrid
authorEmmanuele Bassi <ebassi@gnome.org>
Thu, 4 Apr 2019 21:41:19 +0000 (22:41 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Thu, 4 Apr 2019 21:41:19 +0000 (22:41 +0100)
Getting the layout manager instance out of GtkGrid, and then querying
all layout properties can be tedious, especially for code that was
usually calling gtk_container_child_get().

To replace that, we can add a simple query function that returns the two
attach points and the spans.

gtk/gtkgrid.c
gtk/gtkgrid.h

index 9d4e472559000e7d49352d636bcde5f121c4f9b6..ce67b00609d00a6737981c4f61ba16a18f5ce7e0 100644 (file)
@@ -1075,3 +1075,41 @@ gtk_grid_get_baseline_row (GtkGrid *grid)
 
   return gtk_grid_layout_get_baseline_row (GTK_GRID_LAYOUT (priv->layout_manager));
 }
+
+/**
+ * gtk_grid_query_child:
+ * @grid: a #GtkGrid
+ * @child: a #GtkWidget child of @grid
+ * @left: (out): the column used to attach the left side of @child
+ * @top: (out): the row used to attach the top side of @child
+ * @width: (out): the number of columns @child spans
+ * @height: (out): the number of rows @child spans
+ *
+ * Queries the attach points and spans of @child inside the given #GtkGrid.
+ */
+void
+gtk_grid_query_child (GtkGrid   *grid,
+                      GtkWidget *child,
+                      gint      *left,
+                      gint      *top,
+                      gint      *width,
+                      gint      *height)
+{
+  GtkGridPrivate *priv = gtk_grid_get_instance_private (grid);
+  GtkGridLayoutChild *grid_child;
+
+  g_return_if_fail (GTK_IS_GRID (grid));
+  g_return_if_fail (GTK_IS_WIDGET (child));
+  g_return_if_fail (_gtk_widget_get_parent (child) == (GtkWidget *) grid);
+
+  grid_child = GTK_GRID_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (priv->layout_manager, child));
+
+  if (left != NULL)
+    *left = gtk_grid_layout_child_get_left_attach (grid_child);
+  if (top != NULL)
+    *top = gtk_grid_layout_child_get_top_attach (grid_child);
+  if (width != NULL)
+    *width = gtk_grid_layout_child_get_column_span (grid_child);
+  if (height != NULL)
+    *height = gtk_grid_layout_child_get_row_span (grid_child);
+}
index f40ed7e862e72af21cbbfc04f3cf6f457e80c36e..2ac97cba4e308d98915ea9b7ff2f86eee48c05b5 100644 (file)
@@ -137,6 +137,13 @@ void       gtk_grid_set_baseline_row       (GtkGrid         *grid,
 GDK_AVAILABLE_IN_ALL
 gint       gtk_grid_get_baseline_row       (GtkGrid         *grid);
 
+GDK_AVAILABLE_IN_ALL
+void       gtk_grid_query_child            (GtkGrid         *grid,
+                                            GtkWidget       *child,
+                                            gint            *left,
+                                            gint            *top,
+                                            gint            *width,
+                                            gint            *height);
 
 G_END_DECLS